<?php
App::uses('AppModel', 'Model');
/**
 * Assunto Model
 *
 */
class Assunto extends AppModel {
/**
 * Primary key field
 *
 * @var string
 */
	public $primaryKey = 'id_assuntos';
/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'assunto';
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'id_assuntos' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'assunto' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Não pode ser vazio',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
	
	public function assuntoByCodigo($codigo){
		if(strlen($codigo) >= 6)
			$assunto = substr($codigo, 0, 4);
		else if (strlen($codigo) == 4)
			$assunto = $codigo;
		return $this->find('first',array(
										'conditions'=>array(
											'codigo_prefix'=> $assunto)));
	}
	
	public function visualizacao($id, $valor){
		$this->read(null, $id);
		$this->save(array('visualizacoes'=>$valor+1));
		
	}
}
